#ifndef __H_HTTPHANDLER_H__
#define __H_HTTPHANDLER_H__

#include "../common/decoder.h"
#include "../common/timer.h"
#include <string>
#include <map>
#include <vector>
#include "global.h"
#include "../common/Packet.h"


#define  HTTP_KEEP_ALIVE_TIME_ID   (100)
#define  HTTP_HEART_BEATT_TIME_ID   (101)
#define  HTTP_HEART_BEATT_TIME_CHECK  (30)
#define  HTTP_KEEP_ALIVE_MAX_TIMES   (100)
#define  HTTP_KEEP_ALIVE_TIMEOUT_SEC   (60)
#define  HTTP_KEEP_ALIVE_OPEN (true)

struct stHttpRequest;
struct stHttpResponse;
class HttpHandler;
struct HttpHandlerSetting
{
	std::function<int(HttpHandler*)> on_connected;
	std::function<int(HttpHandler*)> on_close;
	std::function<int(HttpHandler*)> on_timeout;
	std::function<int(HttpHandler*, stHttpRequest&&)> on_message;
};
class HttpHandler : public CCSocketHandler, public TimerOutEvent
{
public:
	HttpHandler(HttpHandlerSetting& handler_setting);
	virtual ~HttpHandler();

public:
	int GetFD() { return netfd; }
	virtual int Init();
	int Close();
    int Response(stHttpResponse&& response);
	
public:
	virtual int InputNotify();
	virtual int OutputNotify();
	virtual int HangupNotify();
	virtual int OnClose();
	virtual int OnConnected();
	virtual int OnTimeout();
	virtual int OnPacketComplete(const char * data, int len);
	virtual int ProcessOnTimerOut(int Timerid);
	connType getConnType(void)
	{
		return CONN_TYPE_ACCEPT;
	}

protected:
	void CheckResponseComplete();

private:
	virtual int Send(CCPacketBase* pOutPkg) { return -1; }  // 回包,直接使用字节流,不使用协议
	virtual ICC_Decoder*  CreateDecoder();
public:
	unsigned int m_HandlerID;
	CCTimer m_heat_beat_timer;
	std::uint32_t m_heat_beat_events = 0;
	HttpHandlerSetting& m_handler_setting;
	void *m_data = nullptr;  //扩展数据 
	bool m_keepalive_active = true;
	std::uint32_t m_keepalive_cnt = 0;
	CCTimer m_keepalive_timer;
};

typedef std::map<unsigned int, HttpHandler*>  HttpHandlerMap;

#endif

